package com.liutaw.nanjingair.views.activities;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.joanzapata.pdfview.PDFView;
import com.joanzapata.pdfview.listener.OnLoadCompleteListener;
import com.liutaw.nanjingair.R;
import com.liutaw.nanjingair.presenter.PDFViewerPresenter;
import com.liutaw.nanjingair.utils.DowloaderTask;
import com.liutaw.nanjingair.utils.FileUtils;
import com.liutaw.nanjingair.views.activities.base.BaseActivity;
import com.liutaw.nanjingair.views.activities.products.IImportantWeatherView;
import com.liutaw.nanjingair.views.viewcomponent.ViewUtils;
import com.liutaw.navigationbarlib.NaviBarBuilder;
import com.szjlxh.data.entity.ImportantWeatherBean;
import com.szjlxh.data.repository.ProductWeatherRepository;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;

public class PDFReaderActivity extends BaseActivity implements IImportantWeatherView {
//    public static final String PDF_URL = "http://10.124.102.170:8080/statellite/static/temppdf/~1.pdf";


    public static Intent getCallingIntent(Context context,String psfurl) {
        Intent intent =  new Intent(context, PDFReaderActivity.class);
        intent.putExtra("data",psfurl);
        return intent;
    }

    private String url;


    private PDFViewerPresenter presenter;
    @Bind(R.id.pdfview)
    PDFView pdfview;

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.layout_pdf_reader);
//    }

    @Override
    protected int getLayoutResId() {
        return R.layout.layout_pdf_reader;
    }

    @Override
    protected void initNaviBar(NaviBarBuilder naviBarBuilder) {

    }

    @Override
    protected String getCusTitle() {
        return "气象预警";
    }

    @Override
    protected void initOnCreate() {
        url = getIntent().getStringExtra("data");
        presenter = new PDFViewerPresenter(this);
        File cacheDir = FileUtils.getExternalCacheDir(this);
        File pdfFile = new File(cacheDir.getAbsolutePath() + PDFViewerPresenter.PDF_FILE_NAME);
        if (pdfFile.exists()) {
            //PDF存在
            onDownloadPdfFileCompleted(pdfFile);
        } else {
            //PDF不存在
            DowloaderTask dowloaderTask = new DowloaderTask(this);
            dowloaderTask.execute(url, cacheDir.getAbsolutePath(), PDFViewerPresenter.PDF_FILE_NAME);
        }

    }

    @Override
    protected void initCusView(View view) {
        ButterKnife.bind(this, view);
    }

    @Override
    public void onDownloadingProgress() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(PDFReaderActivity.this, "下载中...", Toast.LENGTH_LONG).show();
            }
        });

    }

    @Override
    public void onDownloadPdfFileCompleted(final File file) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (file != null && file.exists()) {
//                File file1 = new File(FileUtils.getExternalCacheDir(PDFReaderActivity.this).getAbsolutePath()+"/1.pdf");
                    try {
                        pdfview.fromFile(file).enableSwipe(true).showMinimap(true).onLoad(new OnLoadCompleteListener() {
                            @Override
                            public void loadComplete(int nbPages) {
//                            ViewUtils.showDialogMessage(PDFReaderActivity.this, "下载已完成，" + file.exists() + " " + file.getAbsolutePath());
                            }
                        }).load();
                    } catch (Exception e) {
                        e.printStackTrace();
                        Toast.makeText(PDFReaderActivity.this, "文件已损坏", Toast.LENGTH_LONG).show();
                    }

//                    ViewUtils.showDialogMessage(PDFReaderActivity.this, "下载已完成，" + file.exists() + " " + file.getAbsolutePath());
                } else {
                    Toast.makeText(PDFReaderActivity.this, "文件不存在", Toast.LENGTH_LONG).show();
                }
            }
        });

    }

    @Override
    public void onDownloadingFailed(final String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ViewUtils.showDialogMessage(PDFReaderActivity.this, message, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
            }
        });

    }

    @Override
    public void onMobileDataRemind() {
//        runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                ViewUtils.showDialogMessage(PDFReaderActivity.this, "当前是移动网络，要继续吗？", new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        presenter.requestDownloadFile(PDF_URL, false);
//                    }
//                });
//            }
//        });

    }

    @Override
    public Context getContext() {
        return this;
    }
}
